package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbGoodsDescMapper;
import com.dongdongshop.mapper.TbGoodsMapper;
import com.dongdongshop.mapper.TbItemMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.GoodsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
@DubboService
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private TbGoodsMapper goodsMapper;
    @Resource
    private TbGoodsDescMapper goodsDescMapper;
    @Resource
    private TbItemMapper itemMapper;

    //添加商品
    @Override
    @Transactional
    public void insertGoods(TbGoods goods, TbGoodsDesc goodsDesc,  List<TbItem> item) {
        goodsMapper.insertSelective(goods);
        goodsDesc.setGoodsId(goods.getId());
        goodsDescMapper.insertSelective(goodsDesc);

        for (int i = 0; i < item.size(); i++) {
            item.get(i).setTitle(goods.getCaption());
            item.get(i).setStatus("2");
            item.get(i).setGoodsId(goods.getId());
            item.get(i).setCategoryid(goods.getCategory3Id());
            item.get(i).setCreateTime(new Date());
            item.get(i).setUpdateTime(new Date());
            System.out.println(item.get(i));
            itemMapper.insertSelective(item.get(i));
        }
    }

    //根据主键查询商品
    @Override
    public TbGoods getGoodsById(Long id) {
        return goodsMapper.selectByPrimaryKey(id);
    }

    //根据名称查询商品信息
    @Override
    public List<TbGoods> getGoodsByName(String goodsName) {
        TbGoodsExample example = new TbGoodsExample();
        if (goodsName != null && !"".equals(goodsName)) {
            TbGoodsExample.Criteria criteria = example.createCriteria();
            criteria.andGoodsNameLike("%"+goodsName+"%");
        }
        return goodsMapper.selectByExample(example);
    }

    //删除商品信息
    @Override
    public void deleteGoods(Long[] ids) {
        List<Long> list = Arrays.asList(ids);
        TbGoodsExample example = new TbGoodsExample();
        TbGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(list);
        goodsMapper.deleteByExample(example);
    }

    //驳回商品信息
    @Override
    public void auditNo(Long[] ids,String auditStatus) {
        TbGoods goods = new TbGoods();
        for (Long id : ids) {
            goods.setId(id);
            goods.setAuditStatus(auditStatus);
            goodsMapper.updateByPrimaryKeySelective(goods);
        }
    }

    //审核通过商品信息
    @Override
    public void auditOk(Long[] ids,String auditStatus) {
        TbGoods goods = new TbGoods();
        for (Long id : ids) {
            goods.setId(id);
            goods.setAuditStatus(auditStatus);
            goodsMapper.updateByPrimaryKeySelective(goods);
        }
    }
}
